import iterateStream from '../../utils/iterate-stream';

import StyleManager from '../../xlsx/xform/style/styles-xform';
import WorkbookXform from '../../xlsx/xform/book/workbook-xform';
import RelationshipsXform from '../../xlsx/xform/core/relationships-xform';


class WorkbookReader {
    constructor(input, options = {}) {

        this.input = input;

        this.options = {
            worksheets: 'emit',
            sharedStrings: 'cache',
            hyperlinks: 'ignore',
            styles: 'ignore',
            entries: 'ignore',
            ...options,
        };

        this.styles = new StyleManager();
        this.styles.init();
    }


    async read(input, options) {
        try {
            for await (const { eventType, value } of this.parse(input, options)) {
                switch (eventType) {
                    case 'shared-strings':
                        break;
                    case 'worksheet':
                        await value.read();
                        break;
                    case 'hyperlinks':
                        break;
                }
            }
        } catch (error) {
        }
    }

    _emitEntry(payload) {
        if (this.options.entries === 'emit') {
        }
    }

    async _parseRels(entry) {
        const xform = new RelationshipsXform();
        this.workbookRels = await xform.parseStream(iterateStream(entry));
    }

    async _parseWorkbook(entry) {
        this._emitEntry({ type: 'workbook' });

        const workbook = new WorkbookXform();
        await workbook.parseStream(iterateStream(entry));

        this.properties = workbook.map.workbookPr;
        this.model = workbook.model;
    }

    async _parseStyles(entry) {
        this._emitEntry({ type: 'styles' });
        if (this.options.styles === 'cache') {
            this.styles = new StyleManager();
            await this.styles.parseStream(iterateStream(entry));
        }
    }
}

// for reference - these are the valid values for options
WorkbookReader.Options = {
    worksheets: ['emit', 'ignore'],
    sharedStrings: ['cache', 'emit', 'ignore'],
    hyperlinks: ['cache', 'emit', 'ignore'],
    styles: ['cache', 'ignore'],
    entries: ['emit', 'ignore'],
};

export default WorkbookReader;
